-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Fix assertion when tiling linalg.generic #114688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Fix assertion during tiling due to unsupported linalg.generic containing negative coefficients
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-core Author: Siddhesh Deodhar (siddhesh195) ChangesThis patch fixes assertion during tiling due to unsupported linalg.generic containing negative coefficients in indexing expressions by adding a check before attempting tiling Closes #113021 Full diff: https://github.com/llvm/llvm-project/pull/114688.diff 4 Files Affected:
diff --git a/mlir/include/mlir/IR/AffineMap.h b/mlir/include/mlir/IR/AffineMap.h
index e30950bbf292d6..c2bf0d6b91fed4 100644
--- a/mlir/include/mlir/IR/AffineMap.h
+++ b/mlir/include/mlir/IR/AffineMap.h
@@ -382,6 +382,9 @@ class AffineMap {
/// Returns true if the AffineMap represents a symbol-less permutation map.
bool isPermutation() const;
+ /// Returns true if the AffineMap contains non-positive coefficients
+ bool isNonPositiveCoefficients() const;
+
/// Returns the map consisting of the `resultPos` subset.
AffineMap getSubMap(ArrayRef<unsigned> resultPos) const;
diff --git a/mlir/lib/Dialect/Linalg/Transforms/TilingInterfaceImpl.cpp b/mlir/lib/Dialect/Linalg/Transforms/TilingInterfaceImpl.cpp
index f86715a94b268a..b1e1bf9f721fef 100644
--- a/mlir/lib/Dialect/Linalg/Transforms/TilingInterfaceImpl.cpp
+++ b/mlir/lib/Dialect/Linalg/Transforms/TilingInterfaceImpl.cpp
@@ -119,6 +119,14 @@ struct LinalgOpTilingInterface
// specified could lead to out of bounds accesses.
Location loc = op->getLoc();
LinalgOp linalgOp = cast<LinalgOp>(op);
+ SmallVector<AffineMap> indexingMaps = linalgOp.getIndexingMapsArray();
+ if (llvm::any_of(linalgOp.getIndexingMapsArray(), [](AffineMap m) {
+ return m.isNonPositiveCoefficients();
+ })) {
+ return linalgOp.emitOpError(
+ "tiling not supported because op has a non positive coefficient");
+ }
+
SmallVector<Value> valuesToTile = linalgOp->getOperands();
SmallVector<Value> tiledOperands = makeTiledShapes(
b, loc, linalgOp, valuesToTile, offsets, sizes, {}, true);
diff --git a/mlir/lib/IR/AffineMap.cpp b/mlir/lib/IR/AffineMap.cpp
index ea3c0723b07759..63a2506552b28c 100644
--- a/mlir/lib/IR/AffineMap.cpp
+++ b/mlir/lib/IR/AffineMap.cpp
@@ -9,6 +9,7 @@
#include "mlir/IR/AffineMap.h"
#include "AffineMapDetail.h"
#include "mlir/IR/AffineExpr.h"
+#include "mlir/IR/AffineExprVisitor.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypes.h"
@@ -651,6 +652,28 @@ bool AffineMap::isPermutation() const {
return isProjectedPermutation();
}
+struct CheckCoefficients : public AffineExprVisitor<CheckCoefficients> {
+ CheckCoefficients() {}
+
+ void visitAffineBinaryOpExpr(AffineBinaryOpExpr expr) {
+ visit(expr.getLHS());
+ visit(expr.getRHS());
+ if (expr.getKind() == mlir::AffineExprKind::Mul)
+ isNonPositiveCoefficients |=
+ cast<AffineConstantExpr>(expr.getRHS()).getValue() <= 0;
+ }
+ bool isNonPositiveCoefficients = false;
+};
+
+bool AffineMap::isNonPositiveCoefficients() const {
+
+ return llvm::any_of(getResults(), [](AffineExpr e) {
+ CheckCoefficients t;
+ t.visit(e);
+ return t.isNonPositiveCoefficients;
+ });
+}
+
AffineMap AffineMap::getSubMap(ArrayRef<unsigned> resultPos) const {
SmallVector<AffineExpr, 4> exprs;
exprs.reserve(resultPos.size());
diff --git a/mlir/test/Dialect/Linalg/tile-invalid.mlir b/mlir/test/Dialect/Linalg/tile-invalid.mlir
new file mode 100644
index 00000000000000..1684b1de72c264
--- /dev/null
+++ b/mlir/test/Dialect/Linalg/tile-invalid.mlir
@@ -0,0 +1,29 @@
+// RUN: mlir-opt %s -transform-interpreter -split-input-file -verify-diagnostics
+
+#map1 = affine_map<(d0) -> (d0)>
+#map2 = affine_map<(d0) -> (-d0 + 7)>
+
+func.func @test(%arg0: tensor<8xi8>, %arg1: tensor<8xi8>) -> tensor<8xi8> {
+ // expected-error @below{{tiling not supported because op has a non positive coefficient}}
+ // expected-error @below{{op faild to tile operation}}
+ // expected-error @below{{op failed to generate tiling loops}}
+ %0 = linalg.generic {
+ indexing_maps = [#map2, #map1],
+ iterator_types = ["parallel"]}
+ ins(%arg0 : tensor<8xi8>)
+ outs(%arg1 : tensor<8xi8>) {
+ ^bb0(%in: i8, %out: i8):
+ linalg.yield %in : i8
+ } -> tensor<8xi8>
+ return %0 : tensor<8xi8>
+}
+
+
+module attributes {transform.with_named_sequence} {
+ transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
+ %0 = transform.structured.match ops{["linalg.generic"]} in %arg1 : (!transform.any_op) -> !transform.any_op
+ %1:2 = transform.structured.tile_using_for
+ %0 tile_sizes [2] : (!transform.any_op) -> (!transform.any_op, !transform.any_op)
+ transform.yield
+ }
+}
|
MaheshRavishankar
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. I think it "should" work even with negative indices. What is the error you were hitting? (I can try this locally as well in a bit).
The error we are hitting is due to an assertion here |
What would happen if you remove that assertion? I just wanted to see what the IR looks like if you just let it tile this way? |
|
Removing the assertion will not result in a correct result. Here is the result from test case: |
MaheshRavishankar
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Thanks for the due diligence.
Thank you for the review! If the patch looks good, can you help merge it since I do not have write access |
This patch fixes assertion during tiling due to unsupported linalg.generic containing negative coefficients in indexing expressions by adding a check before attempting tiling
Closes #113021